package com.gem.netty.message;

import com.gem.netty.delay.Heart;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.session.Session;
import com.gem.netty.session.SessionFactory;
import com.gem.utils.IPUtils;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * Author: Xhy
 * CreateTime: 2022-09-25 16:42
 * 绑定通道+重连
 */
public class BindChannelGroupMessage implements Message{
    @Override
    public void handler(ClientContent content) {
        // 玩家是否是重连状态
        Heart heart = Heart.getInstance();
        // 尝试获取用户id
        Session session = SessionFactory.getSession();
        String userId = content.getSendId();
        Channel channel = session.getChannel(userId);
        // 第一次登录
        if (channel == null){
            channel = content.getCtx().channel();
            session.bind(channel,userId);
            Heart.getInstance().addUser(IPUtils.getIp(channel),content.getSendId());
        }else {
            // 重连
            heart.reconnect(IPUtils.getIp(channel));
        }
    }


}
